package com.example.threads.A1B2C3;

import javax.sound.midi.Soundbank;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedTransferQueue;
import java.util.concurrent.TransferQueue;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.LockSupport;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 线程交替打印
 * 用两个线程，一个输出字母，一个输出数字，交替输出 1A2B3C...26Z
 */
public class Title {
    final static char[] aI = "123456789".toCharArray();
    final static char[] aC = "ABCDEFGHI".toCharArray();
    static Thread t1 = null, t2 = null;

    public static void main(String[] args) {
        //test1();
        //test2();
        //test3();
        //test4();
        test5();
    }

    /**
     * LockSupport  内部Jvm实现了观察锁，互斥量
     */
    public static void test1() {
        t1 = new Thread(() -> {
            for (char c : aI) {
                System.out.print(c);
                //叫醒t2
                LockSupport.unpark(t2);
                //阻塞t1，当前线程阻塞
                LockSupport.park();
            }
        }, "t1");

        t2 = new Thread(() -> {
            for (char c : aC) {
                //t2挂起
                LockSupport.park();
                System.out.print(c);
                //唤醒t1
                LockSupport.unpark(t1);
            }
        }, "t2");
        t1.start();
        t2.start();
    }

    /**
     * 同步队列，效率低
     * TransferQueue 是一种容量为0的同步队列
     */
    static TransferQueue<Character> queue = new LinkedTransferQueue<>();

    public static void test2() {
        t1 = new Thread(() -> {
            try {
                for (char c : aI) {
                    System.out.print(queue.take());
                    queue.transfer(c);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "t1");

        t2 = new Thread(() -> {
            try {
                for (char c : aC) {
                    queue.transfer(c);
                    System.out.print(queue.take());
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "t2");
        t1.start();
        t2.start();
    }

    /**
     * wait_notify
     */
    final static Object o = new Object();

    public static void test3() {
        new Thread(() -> {
            synchronized (o) {
                for (char c : aI) {
                    System.out.print(c);
                    try {
                        o.notify();
                        //当前线程wait，让出锁
                        o.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                //不加该代码，无法停止程序
                o.notify();
            }
        }, "t1").start();
        new Thread(() -> {
            synchronized (o) {
                for (char c : aC) {
                    System.out.print(c);
                    try {
                        o.notify();
                        //当前线程wait，让出锁
                        o.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                //不加该代码，无法停止程序
                o.notify();
            }
        }, "t2").start();
    }

    /**
     * 保证优先调用 指定线程
     * CountDownLatch sync wait_notify
     */
    private static CountDownLatch latch = new CountDownLatch(1);

    public static void test4() {
        new Thread(() -> {
            try {
                latch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (o) {
                for (char c : aI) {
                    System.out.print(c);
                    try {
                        o.notify();
                        //当前线程wait，让出锁
                        o.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                //不加该代码，无法停止程序
                o.notify();
            }
        }, "t1").start();
        new Thread(() -> {
            synchronized (o) {
                for (char c : aC) {
                    System.out.print(c);
                    latch.countDown();
                    try {
                        o.notify();
                        //当前线程wait，让出锁
                        o.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                //不加该代码，无法停止程序
                o.notify();
            }
        }, "t2").start();
    }

    /**
     * ReentrantLock Condition
     */
    public static void test5() {
        Lock lock = new ReentrantLock();
        //队列
        Condition condition1 = lock.newCondition();
        Condition condition2 = lock.newCondition();

        CountDownLatch latch = new CountDownLatch(1);
        new Thread(() -> {
            try {
                latch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            lock.lock();
            try {
                for (char c : aC) {
                    System.out.print(c);
                    //精确唤醒队列2
                    condition2.signal();
                    //队列1等待
                    condition1.await();
                }
                condition2.signal();
            }catch(InterruptedException e){
                e.printStackTrace();
            }

        }, "t1").start();
        new Thread(() -> {
            lock.lock();
            try {
                for (char c : aI) {
                    System.out.print(c);
                    latch.countDown();
                    //精确唤醒队列1
                    condition1.signal();
                    //队列2等待
                    condition2.await();
                }
                condition1.signal();
            }catch(InterruptedException e){
                e.printStackTrace();
            }
        }, "t2").start();
    }
}
